import socket
import re
import time
import keys

keys = keys.Keys()
PORT = 777
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
address = ("192.168.4.2", PORT)
server_socket.bind(address)
time.sleep(0.5)
while True:
    receive_data, client_address = server_socket.recvfrom(1024)
    temp = receive_data.decode()
    X = float(re.findall(r"X = (.+?) Y = ", temp)[0])
    Y = float(re.findall(r"Y = (.+?) end", temp)[0])
    print(X, " ", Y)
    if X >= 30:
        keys.directKey("a", type=keys.virtual_keys)
    if X <= -30:
        keys.directKey("d", type=keys.virtual_keys)
    if X > -30 and X < 30:
        keys.directKey("a", keys.key_release, keys.virtual_keys)
        keys.directKey("d", keys.key_release, keys.virtual_keys)
    if Y <= -30:
        keys.directKey("w", type=keys.virtual_keys)
    if Y > -30 and Y < 30:
        keys.directKey("w", keys.key_release, keys.virtual_keys)
        keys.directKey("space", keys.key_release, keys.virtual_keys)
    if Y >= 30:
        keys.directKey("space", type=keys.virtual_keys)
